package divide_and_conquer;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

//https://leetcode.cn/problems/zui-xiao-de-kge-shu-lcof/description/
public class LCR_159库存管理3 {
    //排序
    public int[] inventoryManagement(int[] stock, int cnt) {
        Arrays.sort(stock);
        int[] res = new int[cnt];
        for(int i=0; i<cnt; i++)
            res[i] = stock[i];
        return  res;
    }
    //堆
    public int[] inventoryManagement2(int[] stock, int cnt) {
        int[] res = new int[cnt];
        if(cnt == 0)
            return res;
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for(int i=0; i<cnt; i++)
            queue.add(stock[i]);
        for(int i=cnt; i<stock.length; i++){
            if(queue.peek() > stock[i]){
                queue.poll();
                queue.add(stock[i]);
            }
        }
        for(int i=0; i<cnt; i++)
            res[i] = queue.poll();
        return res;
    }
    //快速排序
}
